﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProjectN.Core.Fore
{
    public partial class ErrorInfoBox : Form
    {
        private DateTime createTime;
        public ErrorInfoBox()
        {
            InitializeComponent();
            this.Text = Properties.Resources.E_TIPBOX_TITLE;
            MessageLabel.Text = Properties.Resources.E_TIPBOX_MSG;
            createTime = DateTime.Now;
        }

        private string? ext_title = null;

        public string? Title
        {
            get => ext_title;
            set
            {
                ext_title = value;
                if (value != null)
                    this.Text = string.Format(Properties.Resources.E_TIPBOX_TITLE_FORMAT, value);
                else
                    this.Text = Properties.Resources.E_TIPBOX_TITLE;
            }
        }

        public string? Message
        {
            get => MessageLabel.Text;
            set
            {
                if (value != null)
                    MessageLabel.Text = value;
                else
                    MessageLabel.Text = Properties.Resources.E_TIPBOX_MSG;
            }
        }

        public required string ExceptionDetail
        {
            get => DetailBox.Text;
            set => DetailBox.Text = value;
        }
        public static void Create(Exception exception, string? title = null, string? msg = null)
            => new ErrorInfoBox { Title = title, Message = msg, ExceptionDetail = exception.ToString() } .Show();
        public static void Create(Form owner, Exception exception, string? title = null, string? msg = null)
            => new ErrorInfoBox { Owner = owner, Title = title, Message = msg, ExceptionDetail = exception.ToString() }.Show();
        private void OkButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            var fn = string.Format("{0}-{1}.txt", ext_title, createTime.ToString("yyyyMMdd-HHmmss"));
            if(fn.IndexOfAny(Path.GetInvalidFileNameChars()) > -1)
                fn = string.Format("{0}-{1}.txt", Properties.Resources.E_TIPBOX_TITLE, createTime.ToString("yyyyMMdd-HHmmss"));
            SaveFileDialog.FileName = fn;

            if (SaveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                try
                {
                    File.WriteAllText(
                        SaveFileDialog.FileName,
                        string.Format("错误信息：{0}\n异常详情：\n{1}", MessageLabel.Text, DetailBox.Text)
                    );
                }
                catch
                {
                    MessageBox.Show(this, "保存失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }

        }
    }

    public static class _Extend_ErrorInfoBox
    {
        public static void CreateErrorInfoBox(this Form owner, Exception exception, string? title = null, string? msg = null)
            => ErrorInfoBox.Create(owner, exception, title, msg);
    }
}
